package com.project.school_flate.serviceimpl.com;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.project.school_flate.dto.com.ComFeedbackDto;
import com.project.school_flate.entity.com.ComFeedback;
import com.project.school_flate.mapper.com.ComFeedbackMapper;
import com.project.school_flate.service.com.ComFeedbackService;
import com.project.school_flate.util.PoToDTO;
import com.project.school_flate.util.Result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务层实现。
 *
 * @author 马维健
 * @since 2024/1/2
 */
@Service
public class ComFeedbackServiceImpl extends ServiceImpl<ComFeedbackMapper, ComFeedback> implements ComFeedbackService {

    @Autowired
    private ComFeedbackMapper oComFeedbackMapper;

    /**
     * 获取反馈
     * @param oComFeedbackDto
     * @return
     * @throws Exception
     */
    @Override
    public Result getComFeedback(ComFeedbackDto oComFeedbackDto) throws Exception {
        List<ComFeedback> oComFeedbackList = new ArrayList<>();
        long total = 0;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderBy("id desc");
        //是否分页
        if(oComFeedbackDto.getPage() != null && oComFeedbackDto.getLimit() != null){
            Page<ComFeedback> ComFeedbackPage = oComFeedbackMapper.paginateWithRelations(oComFeedbackDto.getPage(),oComFeedbackDto.getLimit(),queryWrapper);
            oComFeedbackList = ComFeedbackPage.getRecords();
            total = ComFeedbackPage.getTotalRow();
        }else{
            oComFeedbackList = oComFeedbackMapper.selectListWithRelationsByQuery(queryWrapper);
            total = oComFeedbackList.size();
        }
        //PoToDto
        List<ComFeedbackDto> oComFeedbackDtoList = (List<ComFeedbackDto>) PoToDTO.poToDtoList(oComFeedbackList,new ComFeedbackDto());
        return Result.ok(oComFeedbackDtoList,total);
    }

    /**
     * 添加反馈
     * @param oComFeedbackDto
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Result addComFeedback(ComFeedbackDto oComFeedbackDto) throws Exception {
        ComFeedback oComFeedback = new ComFeedback();
        PoToDTO.poToDto(oComFeedbackDto,oComFeedback);
        if(oComFeedbackMapper.insert(oComFeedback) == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error("添加反馈失败");
        }
        return Result.ok("添加反馈成功");
    }

    /**
     * 修改反馈
     * @param oComFeedbackDto
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Result updateComFeedback(ComFeedbackDto oComFeedbackDto) throws Exception {
        ComFeedback oComFeedback = new ComFeedback();
        PoToDTO.poToDto(oComFeedbackDto,oComFeedback);
        if(oComFeedbackMapper.update(oComFeedback) == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error("修改反馈失败");
        }
        return Result.ok("修改反馈成功");
    }

    /**
     * 删除反馈
     * @param oComFeedbackDto
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Result deleteComFeedback(ComFeedbackDto oComFeedbackDto) throws Exception {
        ComFeedback oComFeedback = new ComFeedback();
        PoToDTO.poToDto(oComFeedbackDto,oComFeedback);
        if(oComFeedbackMapper.deleteById(oComFeedback.getId()) == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error("删除反馈失败");
        }
        return Result.ok("删除反馈成功");
    }

    /**
     * 回复反馈
     * @param oComFeedbackDto
     * @return
     * @throws Exception
     */
    @Override
    public Result replyComFeedback(ComFeedbackDto oComFeedbackDto) throws Exception {
        return null;
    }

}
